package com.atguigu.gulimall.order.rabbit.callback;

import com.atguigu.gulimall.order.entity.MqMessageEntity;
import com.atguigu.gulimall.order.rabbit.constant.OrderRabbitConstant;
import com.atguigu.gulimall.order.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Objects;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/16 21:57
 */
@Slf4j
@Component
public class RabbitCallback implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback{
    
    @Autowired
    RabbitTemplate rabbitTemplate;
    
    @Autowired
    MqMessageService messageService;
    
    
    @PostConstruct
    public void init() {
        log.info("init 注入confirmCallback,returnCallback");
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     * 消息发送到交换机确认回调
     * @param correlationData correlation data for the callback.
     * @param ack true for ack, false for nack
     * @param cause An optional cause, for nack, when available, otherwise null.
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.error("returnedMessage message no arrival exchange message: {}, ack: {}, cause: {}", correlationData, ack, cause);
    }
    
    /**
     * 消息发送到队列确认回调 抵达
     * @param message the returned message. 消息
     * @param replyCode the reply code. 回复码
     * @param replyText the reply text. 回退内容
     * @param exchange the exchange. 交换机
     * @param routingKey the routing key. 路由键
     */
   /* @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.error("returnedMessage message no arrival queue message: {}, replyText: {}, exchange: {}, routingKey: {}", new String(message.getBody(), StandardCharsets.UTF_8), replyText, exchange, routingKey);
        if (OrderRabbitConstant.ORDER_RELEASE_ORDER_QUEUE.equals(routingKey)) {
            String messageId = message.getMessageProperties().getMessageId();
            MqMessageEntity mqMessageEntity = messageService.getById(messageId);
            if (Objects.isNull(mqMessageEntity)) {
                throw new IllegalArgumentException("没有发现消息记录，请确定是否发送该消息");
            }
            messageService.updateById(MqMessageEntity.builder().messageId(messageId).messageStatus(3).build());
        }
    }*/

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        log.info("returnedMessage receive message body: {},replyCode: {},replyText: {}, exchange: {}, routingKey: {}", new String(returned.getMessage().getBody(), StandardCharsets.UTF_8), returned.getReplyCode(), returned.getReplyText(), returned.getExchange(), returned.getRoutingKey());
        String msg = new String(returned.getMessage().getBody(), StandardCharsets.UTF_8);
        //应答码
        int replyCode = returned.getReplyCode();
        //应答描述
        String replyText = returned.getReplyText();
        //应答交换机
        String exchange = returned.getExchange();
        //应答routingKey
        String routingKey = returned.getRoutingKey();
        if (OrderRabbitConstant.ORDER_RELEASE_ORDER_QUEUE.equals(routingKey)) {
//            String messageId = returned.getMessage().getMessageProperties().getMessageId();
//            MqMessageEntity mqMessageEntity = messageService.getById(messageId);
//            if (Objects.isNull(mqMessageEntity)) {
//                throw new IllegalArgumentException("没有发现消息记录，请确定是否发送该消息");
//            }
//            messageService.updateById(MqMessageEntity.builder().messageId(messageId).messageStatus(3).build());
        }
    }
}
